package com.yanqu.road.logic.activity.foxcard.game.spell;

import com.yanqu.road.entity.activity.foxcard.FoxSaleCard;
import com.yanqu.road.entity.activity.foxcard.config.FoxCardMagicWeaponConfig;
import com.yanqu.road.entity.activity.foxcard.config.FoxCardSorceryConfig;
import com.yanqu.road.entity.activity.foxcard.config.FoxCardSpellConfig;
import com.yanqu.road.logic.activity.foxcard.FoxCardConfig;
import com.yanqu.road.logic.activity.foxcard.game.FoxCardGameLogic;
import com.yanqu.road.logic.activity.foxcard.game.eFoxCardCouponType;
import com.yanqu.road.logic.activity.foxcard.game.eFoxCardType;

import java.util.HashSet;
import java.util.List;

/**
 * 随机生成资源牌数量
 * 数量|资源牌类型
 */
public class FoxCardSpellUse2 extends FoxCardSpellUseBase {
    @Override
    protected boolean spellUse(FoxCardGameLogic gameLogic) {
        int cardNum = spellConfig.getParamList().get(0);
        int cardType = spellConfig.getParamList().get(1);
        int consumeCardNumLimit = gameLogic.getGameData().getConsumeCardNumLimit();
        if (consumeCard != null) {//因为自身会销毁,所以上限+1
            consumeCardNumLimit += 1;
        }

        FoxCardConfig config = gameLogic.getConfig();
        if (cardType == eFoxCardType.EFFECT.getValue()) {
            List<FoxSaleCard> foxSaleCards = gameLogic.getConfig().randomEffectCard(gameLogic.getGameData().getEffectCards(), 0, cardNum, -1, gameLogic.getCouponAddValue(eFoxCardCouponType.COUPON8.getValue()));
            for (FoxSaleCard foxSaleCard : foxSaleCards) {
                if (!config.isBuffType8(foxSaleCard.getVersionEffect()) && gameLogic.getGameData().getEffectCards().size() >= gameLogic.getGameData().getEffectCardNumLimit()) {
                    continue;
                }
                gameLogic.addNewEffectCard(foxSaleCard.getConfigId(), foxSaleCard.getVersionEffect());

            }
        } else if (cardType == eFoxCardType.HAND.getValue()) {
            for (int i = 0; i < cardNum; i++) {
                FoxSaleCard foxSaleCard = gameLogic.randomSaleHandCard();
                gameLogic.addNewHandCard(foxSaleCard.getPoint(), foxSaleCard.getColor(), foxSaleCard.getReinforceEffect(), foxSaleCard.getVersionEffect(), foxSaleCard.getSealEffect());
            }
        } else if (cardType == eFoxCardType.MAGIC_WEAPON.getValue()) {
            if (consumeCard != null && config.isBuffType8(consumeCard.getVersionEffect()) && gameLogic.getGameData().getConsumeCards().size() >= gameLogic.getGameData().getEffectCardNumLimit()) {
                //本身有负片效果，且已经达到上限,直接用掉
                return true;
            }
            List<FoxCardMagicWeaponConfig> magicWeaponConfigs = config.randomMagicWeapons(cardNum, new HashSet<>());
            for (FoxCardMagicWeaponConfig magicWeaponConfig : magicWeaponConfigs) {
                if (gameLogic.getGameData().getConsumeCards().size() >= consumeCardNumLimit) {
                    break;
                }
                gameLogic.addNewConsumeCard(cardType, magicWeaponConfig.getId(), 0);
            }
        } else if (cardType == eFoxCardType.SPELL.getValue()) {
            if (consumeCard != null && config.isBuffType8(consumeCard.getVersionEffect()) && gameLogic.getGameData().getConsumeCards().size() >= gameLogic.getGameData().getEffectCardNumLimit()) {
                //本身有负片效果，且已经达到上限,直接用掉
                return true;
            }
            List<FoxCardSpellConfig> spellConfigs = config.randomSpells(cardNum);
            for (FoxCardSpellConfig spellConfig : spellConfigs) {
                if (gameLogic.getGameData().getConsumeCards().size() >= consumeCardNumLimit) {
                    break;
                }
                gameLogic.addNewConsumeCard(cardType, spellConfig.getId(), 0);
            }
        } else if (cardType == eFoxCardType.SORCERY.getValue()) {
            if (consumeCard != null && config.isBuffType8(consumeCard.getVersionEffect()) && gameLogic.getGameData().getConsumeCards().size() >= gameLogic.getGameData().getEffectCardNumLimit()) {
                //本身有负片效果，且已经达到上限,直接用掉
                return true;
            }
            List<FoxCardSorceryConfig> foxCardSorceryConfigs = config.randomSorceryList(cardNum);
            for (FoxCardSorceryConfig foxCardSorceryConfig : foxCardSorceryConfigs) {
                if (gameLogic.getGameData().getConsumeCards().size() >= consumeCardNumLimit) {
                    break;
                }
                gameLogic.addNewConsumeCard(cardType, foxCardSorceryConfig.getId(), 0);
            }
        } else {
            return false;
        }

        return true;
    }
}
